//启动app
var app = angular.module("myApp", []);

//启动文本编辑器
KindEditor.ready(function(K) {
	window.editor = K.create('#editor');
});

//启动颜色选择器
$('#colorSelector').ColorPicker({
	color: '#0000ff',
	onShow: function (colpkr) {
		$(colpkr).fadeIn(500);
		return false;
	},
	onHide: function (colpkr) {
		$(colpkr).fadeOut(500);
		return false;
	},
	onChange: function (hsb, hex, rgb) {
		$('#colorSelector div').css('backgroundColor', '#' + hex);
	}
});

/**
 * 编辑图表标题
 */
function editTitle(){
	document.getElementById('editorDiv').style.display = "";
}

/**
 * 提交图表标题修改
 */
function editTitleSubmit(){
	document.getElementById('editorDiv').style.display = "none";
	document.getElementById('chartTitle').innerHTML=editor.html();
}

/**
 * 切换X轴标签显示
 */
function changeXLabelType(btn){
	if(document.getElementById('xDivLabel').style.display == ""){
		/*document.getElementById('xSVGLabel').style.display = "";
		document.getElementById('xSVGScale').style.display = "";*/
		document.getElementById('xDivLabel').style.display = "none";
		document.getElementById('xLabelButtons').style.display = "none";
		btn.innerHTML="显示X轴标签";
		return;
	}
	/*document.getElementById('xSVGLabel').style.display = "none";
	document.getElementById('xSVGScale').style.display = "none";*/
	document.getElementById('xDivLabel').style.display = "";
	document.getElementById('xLabelButtons').style.display = "";
	btn.innerHTML="隐藏X轴标签";
}

/**
 * 切换X轴刻度显示
 */
function changeXScaleType(btn){
	if(document.getElementById('xSVGScale').style.display == "none"){
		document.getElementById('xSVGScale').style.display = "";
		btn.innerHTML="隐藏X轴刻度";
		return;
	}
	document.getElementById('xSVGScale').style.display = "none";
	btn.innerHTML="显示X轴刻度";
}

/**
 * 切换Y轴标签显示
 */
function changeYLabelType(btn){
	if(document.getElementById('yDivLabel').style.display == ""){
		/*document.getElementById('xSVGLabel').style.display = "";
		document.getElementById('xSVGScale').style.display = "";*/
		document.getElementById('yDivLabel').style.display = "none";
		document.getElementById('yLabelButtons').style.display = "none";
		btn.innerHTML="显示Y轴标签";
		return;
	}
	/*document.getElementById('xSVGLabel').style.display = "none";
	document.getElementById('xSVGScale').style.display = "none";*/
	document.getElementById('yDivLabel').style.display = "";
	document.getElementById('yLabelButtons').style.display = "";
	btn.innerHTML="隐藏Y轴标签";
}

/**
 * 修改X轴标签文字的方法
 */
function editXLi(li){
	li.innerHTML="<input type='text' style='text-align: center;' class='form-control' value='" + li.getElementsByTagName("a")[0].innerHTML + "' />";
	li.getElementsByTagName("input")[0].onblur = function(){
		li.innerHTML="<a>"+li.getElementsByTagName("input")[0].value+"</a>";
	};
}

/**
 * 修改Y轴标签文字的方法
 */
function editYLi(li){
	li.innerHTML="<input type='text' style='text-align: center;' class='form-control' value='" + li.innerHTML + "' />";
	li.onclick=function(){};
	li.getElementsByTagName("input")[0].onblur = function(){
		li.innerHTML=li.getElementsByTagName("input")[0].value;
		li.setAttribute("onclick","editYLi(this)");
	};
}

/**
 * 增加x轴标签的方法
 */
function addXLabel(){
	var ul=document.getElementById('xLabelsUl');
	var li=document.createElement("li");
	var a=document.createElement("a"); 
    a.innerHTML="新标签";
    li.appendChild(a);
    li.setAttribute("onclick","editXLi(this)");
    ul.appendChild(li);  
}

/**
 * 增加y轴标签的方法
 */
function addYLabel(){
	var ul=document.getElementById('yLabelsUl');
	var a=document.createElement("a");  
    a.innerHTML="新标签";
    ul.appendChild(a);
    var li=document.getElementById("yLabelsUl").getElementsByTagName("a");
    for(var i=0;i<li.length;i++){
    	li[i].style.height=(500+li.length)/li.length-0.1+"px";
    	li[i].setAttribute("onclick","editYLi(this)");
    	li[i].setAttribute("class","list-group-item");
    	if(parseInt(((500+li.length)/li.length-0.1)/2-li.length/2)<=0){
    		li[i].style.lineHeight="0.5px";
    	}
    	else{
    		li[i].style.lineHeight=((500+li.length)/li.length-0.1)/2-li.length/2+"px";
    	}
    }
}

/**
 * 删除x轴标签的方法
 */
function deleteXLabel(){
	var li=document.getElementById("xLabelsUl").getElementsByTagName("li");
	$($("#xLabelsUl").find("li")[li.length-1].remove());
}

/**
 * 删除y轴标签的方法
 */
function deleteYLabel(){
	var li=document.getElementById("yLabelsUl").getElementsByTagName("a");
	$($("#yLabelsUl").find("a")[li.length-1].remove());
    var li=document.getElementById("yLabelsUl").getElementsByTagName("a");
    for(var i=0;i<li.length;i++){
    	li[i].style.height=(500+li.length)/li.length-0.1+"px";
    	li[i].setAttribute("onclick","editYLi(this)");
    	li[i].setAttribute("class","list-group-item");
    	if(parseInt(((500+li.length)/li.length-0.1)/2-li.length/2)<=0){
    		li[i].style.lineHeight="0.5px";
    	}
    	else{
    		li[i].style.lineHeight=((500+li.length)/li.length-0.1)/2-li.length/2+"px";
    	}
    }
}

/**
 * 点击切换透明度的方法
 */
function changeOpacity(obj){
	if(obj.style.opacity!=0.1){
		obj.style.opacity=0.1;
		return;
	}
	obj.style.opacity=null;
}

/**
 * 格式化float位数的方法
 * @param src 原float
 * @param pos 小数位数
 * @returns {Number} 格式化后的float
 */
function formatFloat(src, pos){
    return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos);
}
 